package com.cleaningcloud.cld.controller.mini;

import com.cleaningcloud.cld.domain.CldComplaintsReply;
import com.cleaningcloud.cld.domain.dto.CldComplaintsDto;
import com.cleaningcloud.cld.domain.vo.CldComplaintsVo;
import com.cleaningcloud.cld.mapper.CldComplaintsReplyMapper;
import com.cleaningcloud.cld.service.ICldComplaintsReplyService;
import com.cleaningcloud.cld.service.ICldComplaintsService;
import com.cleaningcloud.common.core.utils.DateUtils;
import com.cleaningcloud.common.core.web.controller.BaseController;
import com.cleaningcloud.common.core.web.domain.AjaxResult;
import com.cleaningcloud.common.core.web.page.TableDataInfo;
import com.cleaningcloud.common.security.utils.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 物资管理 - 小程序接口
 */
@RestController
@RequestMapping("/mini/complaints")
public class MiniComplaintsController extends BaseController {

    @Autowired
    private ICldComplaintsService complaintsService;

    @Autowired
    private ICldComplaintsReplyService cldComplaintsReplyService;

    @Autowired
    private CldComplaintsReplyMapper cldComplaintsReplyMapper;


    @Operation(description = "客诉列表查看")
    @GetMapping("/getComplaintsList")
    public TableDataInfo getComplaintsList(CldComplaintsDto cldComplaintsDto) {
        startPage();
        List<CldComplaintsVo> list = complaintsService.selectCldComplaintsList(cldComplaintsDto);
        return getMiniDataTable(list);
    }

    @Operation(description = "客户新建投诉与建议")
    @PostMapping("/addComplaints")
    public AjaxResult addComplaints(@RequestBody CldComplaintsDto cldComplaintsDto) {
        return toAjax(complaintsService.insertCldComplaints(cldComplaintsDto));
    }

    @Operation(description = "下发整改")
    @PutMapping("/IssueRectification")
    public AjaxResult IssueRectification(@RequestBody CldComplaintsDto cldComplaintsDto) {
        cldComplaintsDto.setAssignTime(DateUtils.getNowDate());
        cldComplaintsDto.setHandleStatus(1L);
        cldComplaintsDto.setHandleStaffId(cldComplaintsDto.getAssignStaffId());
        CldComplaintsReply cldComplaintsReply = new CldComplaintsReply();
        cldComplaintsReply.setReplyStaffId(SecurityUtils.getUserId());
        cldComplaintsReply.setReplyUserType(1L);
        cldComplaintsReply.setReplyContent("下发给" + cldComplaintsDto.getAssignStaffName());
        cldComplaintsReply.setComplanintsId(cldComplaintsDto.getComplaintsId());
        cldComplaintsReply.setReplyTime(DateUtils.getNowDate());
        cldComplaintsReplyMapper.insertCldComplaintsReply(cldComplaintsReply);
        return toAjax(complaintsService.updateCldComplaints(cldComplaintsDto));
    }

    @Operation(description = "立即反馈")
    @PutMapping("/ImmediateFeedback")
    public AjaxResult ImmediateFeedback(@RequestBody CldComplaintsReply cldComplaintsReply) {
        return toAjax(cldComplaintsReplyService.insertCldComplaintsReply(cldComplaintsReply));
    }

    @Operation(description = "客户审核")
    @PutMapping("/auditComplaints")
    public AjaxResult auditComplaints(@RequestBody CldComplaintsDto cldComplaintsDto) {
        CldComplaintsReply cldComplaintsReply = new CldComplaintsReply();
        cldComplaintsReply.setReplyStaffId(SecurityUtils.getUserId());
        cldComplaintsReply.setReplyTime(DateUtils.getNowDate());
        if (cldComplaintsDto.getHandleStatus() == 3) {
            cldComplaintsDto.setRejectTime(DateUtils.getNowDate());
            cldComplaintsDto.setHandleStatus(1L);
            cldComplaintsReply.setReplyContent(cldComplaintsDto.getRejectText());
        }
        if (cldComplaintsDto.getHandleStatus() == 4) {
            cldComplaintsReply.setReplyContent(cldComplaintsDto.getSovleText());
        }
        if (cldComplaintsDto.getHandleStatus() == 5) {
            cldComplaintsReply.setReplyContent(cldComplaintsDto.getRevokeText());
        }
        cldComplaintsReply.setReplyUserType(3L);
        cldComplaintsReply.setComplanintsId(cldComplaintsDto.getComplaintsId());
        cldComplaintsReply.setReplyImgs(cldComplaintsDto.getRejectImg());
        cldComplaintsReplyMapper.insertCldComplaintsReply(cldComplaintsReply);
        // 修改客诉信息
        return toAjax(complaintsService.updateCldComplaints(cldComplaintsDto));
    }

    @Operation(description = "客诉详情查看")
    @GetMapping("/getComplaintsDetails/{complaintsId}")
    public AjaxResult getComplaintsDetails(@PathVariable Long complaintsId) {
        return success(complaintsService.selectCldComplaintsByComplaintsId(complaintsId));
    }
}
